package com.bzzoneit.newsys.service.admin.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bzzoneit.newsys.dao.admin.MenuMapper;
import com.bzzoneit.newsys.entity.admin.Menu;
import com.bzzoneit.newsys.service.admin.MenuService;
import com.bzzoneit.newsys.utils.exception.ServiceException;

@Service
public class MenuServiceImpl implements MenuService {
	
	protected static Logger logger =  LoggerFactory.getLogger("service");
	@Autowired
	private MenuMapper menuMapper;
	
	/* (non-Javadoc)
	 * @see com.bzzoneit.newsys.service.admin.impl.MenuService#getMenuListByUserId(java.lang.Long)
	 */
	@Transactional
	public List<Menu> getMainMenuList(Long userId) {
		if(isAdmin(userId)){
			return menuMapper.getMainMenuList();
		}else
		{
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("userId", userId);
			param.put("id", null);
			return menuMapper.getSubMenuList(param);
		}
	}
	
	public List<Menu> getListByParentId(Long userId,Long parentId) {
		Map<String,Object> param = new HashMap<String,Object>();
		List<Menu> mainMenuList = menuMapper.getMenuList(parentId);
		Iterator<Menu> it = mainMenuList.iterator();
		//装载主菜单下所有的子菜单
		while(it.hasNext()) {
			Menu menu = it.next();
			//false:表示为主菜单
			menu.setLeaf(false);
			Long id = menu.getId();
			param.put("userId", userId);
			param.put("id", id);
			List<Menu> subMenuList = menuMapper.getSubMenuList(param);
			menu.setChildren(subMenuList);
		}
		return mainMenuList;

	}

	public List<Menu> getSubMenuList(Long userId,Long parentId) {	
		//id为1的为超级管理员  具有所有权限
		if(isAdmin(userId)){
			return menuMapper.getMenuList(parentId);
		}
		else
		{
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("userId", userId);
			param.put("id", parentId);
			return menuMapper.getSubMenuList(param);
		}
	}

	private boolean isAdmin(Long userId) {
		return userId==1L;
	}

	public List<Menu> getAllMenuList() {
		// TODO Auto-generated method stub
		return menuMapper.getAllMenuList();
	}

	public void updateOrinsertMenu(Menu menu) {
		Menu m=menuMapper.selectOne(menu.getId());
		if(m==null){
			menuMapper.insertOne(menu);
		}
		else{
			menuMapper.updateOne(menu);
		}
	}

	@Transactional
	public void deleteIds(List<Long> ids) {	
		if(ids.indexOf(1L)>0){
			throw new ServiceException("不能删除超级管理员");
		}
		else{
		menuMapper.deleteIds(ids);		//t_menu
		menuMapper.deleteRoleMenuIds(ids);//t_role_menu
		}
	}

	public Menu getMenu(Long id) {
		return menuMapper.selectOne(id);
	}



	
	private StringBuilder treeString=new StringBuilder("");
	
	public String getTreeStringByParentId(Long userId,Long parentId,boolean isinSide) {
		List<Menu> subMenuList = getSubMenuList(userId,parentId);
		for(Menu m:subMenuList){
			//判断是否是叶子 就是没有子菜单
			if(isisLeaf(userId,m.getId())){
				treeString.append("<li>")
				.append("<a href=\"").append(m.getUrl()).append("?navtabid=").append(m.getNavtabid())
				.append("\"  target=\"navTab\" rel=\"")
				.append(m.getNavtabid())
				.append("\" >")
				.append(m.getName())
				.append("</a>")
				.append("</li>");
			}
			else{
				treeString.append("<li><a>").append(m.getName()).append("</a><ul>");
				getTreeStringByParentId(userId,m.getId(),true);
				treeString.append("</ul>").append("</li>");
			}
			
			System.out.println("是不是内部调用:"+isinSide+" id:"+m.getId());
		}	
		String result= treeString.toString();
		if(!isinSide){
			treeString.setLength(0);		
		}
		return result;
	}
	
	public boolean isisLeaf(Long userId,Long parentId){
		List<Menu> subMenuList =getSubMenuList(userId,parentId);
		//有子菜单就不是叶子节点
		if(subMenuList!=null&&subMenuList.size()>0){
			return false;
		}
		else{
			return true;
		}
	}	
}
